package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.RecommendSongsRoot;

import java.util.List;


/**
 * Created by dllo on 15/10/24.
 */
public class RecommendSongsAdapter extends BaseAdapter{
    private Context context;
    private List<RecommendSongsRoot.DataEntity> entityArrayList;

    public RecommendSongsAdapter(Context context, List<RecommendSongsRoot.DataEntity> entityArrayList) {
        this.context = context;
        this.entityArrayList = entityArrayList;
    }

    @Override
    public int getCount() {
        return entityArrayList == null ? 0 : entityArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return entityArrayList == null ? null : entityArrayList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.songs_list_listview_item, null);
            holder = new ViewHolder();
            holder.songs_list_position = (TextView) convertView.findViewById(R.id.songs_list_position);
            holder.songs_list_songName = (TextView) convertView.findViewById(R.id.songs_list_songName);
            holder.songs_list_item_singer = (TextView) convertView.findViewById(R.id.songs_list_item_singer);
            holder.songs_list_item_number = (TextView) convertView.findViewById(R.id.songs_list_item_number);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        int songs_list_position = position % entityArrayList.size() + 1;
        holder.songs_list_position.setText(String.valueOf(songs_list_position));
        holder.songs_list_songName.setText(entityArrayList.get(position % entityArrayList.size()).getName());
        holder.songs_list_item_singer.setText(entityArrayList.get(position % entityArrayList.size()).getSingerName());
        holder.songs_list_item_number.setText(String.valueOf(entityArrayList.get(position % entityArrayList.size()).getFavorites()));
        return convertView;
    }

    private class ViewHolder{
        private TextView songs_list_position, songs_list_songName
                , songs_list_item_singer, songs_list_item_number;
    }
}
